package com.goldplusgold.tradedelay.app.base.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.WindowManager;

import com.example.account.manager.AppCacheSharedPreferences;
import com.goldplusgold.tradedelay.app.main.activity.TDMainActivity;
import com.goldplusgold.support.lib.hybird.WebViewActivity;
import com.goldplusgold.tradedelay.app.base.constant.IntentConstant;
import com.goldplusgold.tradedelay.app.my.activity.UserActivity;
import com.goldplusgold.tradedelay.app.my.activity.UserWebviewActivity;
import com.goldplusgold.tradedelay.app.my.activity.WechatRegisterActivity;
import com.goldplusgold.tradedelay.app.my.networkservice.MyService;
import com.goldplusgold.tradedelay.app.trade.activity.TradeWebViewActivity;
import com.goldplusgold.tradedelay.app.trade.networkservice.ApiConfig;
import com.goldplusgold.tradedelay.app.base.widget.MyAlertDialog;

public class ActivityUtils {
    //挤出登录弹框是否已经显示
    public static boolean isShowForcequitDialog = false;

    public static void gotoTradeLogin(Activity context) {
        Intent intent = new Intent(context, TradeWebViewActivity.class);
        intent.putExtra("url", ApiConfig.TRADE_LOGIN);
        context.startActivity(intent);
    }

    public static void gotoWechatRegister(Activity context, String phone, String wechatCode) {
        Intent intent = new Intent(context, WechatRegisterActivity.class);
        intent.putExtra("phone", phone);
        intent.putExtra("wechatCode", wechatCode);
        context.startActivity(intent);
    }

    public static void skipToTDMain(Context context, String type) {
        Intent intent = new Intent(context, TDMainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(IntentConstant._TYPE, type);
        context.startActivity(intent);
    }


    public static void gotoActionBrowserPage(Activity activity, String url) {
        Intent intent = new Intent(activity, WebViewActivity.class);
        intent.putExtra("url", url);
        activity.startActivity(intent);
    }

    public static void gotoUserWebview(Activity activity, String url) {
        Intent intent = new Intent(activity, UserWebviewActivity.class);
        intent.putExtra("url", url);
        activity.startActivity(intent);
    }

    public static void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().setAttributes(lp);
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }

    public static void forceQuit(final Activity context, String text) {
        if (!isShowForcequitDialog) {
            if (MyService.getInstance().isLogin()) {
                new MyAlertDialog(context).builder().setMsg(text).setCancelable(false)
                        .setPositiveButton("确定", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                AppCacheSharedPreferences.clearUserInfo();
                                MyService.getInstance().cleanToken();
                                Intent intent = new Intent(context, UserActivity.class);
                                intent.putExtra(IntentConstant._TYPE, "forceQuit");
                                context.startActivity(intent);
                            }
                        }).show();
            }
        }
        isShowForcequitDialog = true;
    }

    public static void isQuit(final Activity context , String text){
            if (MyService.getInstance().isLogin()) {
                new MyAlertDialog(context).builder().setMsg(text).setCancelable(false)
                        .setPositiveButton("确定", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                AppCacheSharedPreferences.clearUserInfo();
                                MyService.getInstance().cleanToken();
                                Intent intent = new Intent(context, UserActivity.class);
                                intent.putExtra(IntentConstant._TYPE, "isQuit");
                                context.startActivity(intent);
                            }
                        }).show();
            }
    }
}
